package Connect;

import java.sql.*;



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.sql.*;
import javax.naming.*;

public class connector extends HttpServlet{

	 /**
	 * 
	 */
	private static final long serialVersionUID = 5840401811311847118L;
	private static DataSource ds;
	
	public void init()throws ServletException{
		try{
		Context initCtx = new InitialContext();
		Context envCtx = (Context)initCtx.lookup("java:comp/env");
		ds = (DataSource)envCtx.lookup("jdbc/KOMORIUTA");
		}catch(Exception e){
			e.printStackTrace();
		}
		//return ds.getConnection();
	}
	public static Connection getConnection(){
		try{
			return ds.getConnection();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public static void releaseConnection(Connection conn, PreparedStatement pstmt, ResultSet rs){
		if ( rs != null ) try{rs.close();}catch(Exception e){
			e.printStackTrace();
		} 
	    if ( pstmt != null ) try{pstmt.close();}catch(Exception e){
	    	e.printStackTrace();
	    }
	    if ( conn != null ) try{conn.close();}catch(Exception e){
	    	e.printStackTrace();
	    }
	}
	
	public static void releaseConnection(Connection conn, Statement stmt, ResultSet rs){
		if ( rs != null ) try{rs.close();}catch(Exception e){
			e.printStackTrace();
		} 
	    if ( stmt != null ) try{stmt.close();}catch(Exception e){
	    	e.printStackTrace();
	    }
	    if ( conn != null ) try{conn.close();}catch(Exception e){
	    	e.printStackTrace();
	    }
	}
	public static void releaseConnection(Connection conn, PreparedStatement pstmt) {
		 if ( pstmt != null ) try{pstmt.close();}catch(Exception e){
			 e.printStackTrace();
		 }
		 if ( conn != null ) try{conn.close();}catch(Exception e){
			 e.printStackTrace();
		 }
	}
	public static void releaseConnection(Connection conn, Statement stmt) {
		 if ( stmt != null ) try{stmt.close();}catch(Exception e){
			 e.printStackTrace();
		 }
		 if ( conn != null ) try{conn.close();}catch(Exception e){
			 e.printStackTrace();
		 }
	}
	
}
